jQuery(function ($) {
    /** 表单提交* */
    $("#edit_submit").on('click', (function () {
        $('#edit_form').data('bootstrapValidator').resetForm();//清理历史错误
        $('#edit_form').bootstrapValidator('validate');
        if ($('#edit_form').data('bootstrapValidator').isValid()) {
            var id = $('#inputId').val();
            var activityName = $('#inputActivityName').val();
            var status = $('#inputStatus').val();
            var startTime = $('#inputStartTime').val();
            var endTime = $('#inputEndTime').val();
            var activityInfo = $('#inputActivityInfo').val();
            var ranging = $('#inputRanging').val();
            var param = {
                id: id,
                activityName: activityName,
                status: status,
                startTime: startTime,
                endTime: endTime,
                activityInfo: activityInfo,
                ranging: ranging
            };

            var lens = $("#inputItems").find("input[type='text']").length;
            for (var i = 0; i < lens; i++) {
                var item = $('#inputItem' + i).val();
                if (item === undefined || item.trim() === '') {
                    toastr.error("活动选项不能为空");
                    return;
                }
                param['items[' + i + '].itemValue'] = item;
            }

            if (ranging == null || ranging === '') {
                toastr.error("活动范围不能为空");
                return;
            }

            $("#edit_submit").attr({"disabled": "disabled"});
            $.ajax({
                type: "POST",
                async: false,
                dataType: "json",
                url: "/activity/save",
                data: param,
                success: function (data) {
                    if (data.code === 1) {
                        $('#modal-default').modal("hide");
                        toastr.success("操作成功");
                        $('#dynamic-table').DataTable().ajax.reload(); //重新载入
                    } else {
                        toastr.error(data.msg);
                    }
                    $("#edit_submit").removeAttr("disabled");
                }
            });
            $("#edit_submit").removeAttr("disabled");
        }
        return false;
    }));

    $('#edit_form').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            activityName: {
                selector: '#inputActivityName',
                validators: {
                    notEmpty: {message: '不能为空！'}
                }
            }, startTime: {
                selector: '#inputStartTime',
                validators: {
                    notEmpty: {message: '不能为空！'}
                }
            }, endTime: {
                selector: '#inputEndTime',
                validators: {
                    notEmpty: {message: '不能为空！'}
                }
            }, status: {
                selector: '#inputStatus',
                validators: {
                    notEmpty: {message: '不能为空！'}
                }
            }
        }
    });

    $("#addItem").on('click', function () {
        var length = $("#inputItems").find("input[type='text']").length;
        var html = '<input type="text" id="inputItem' + length + '" class="add-item"/>';
        html += '<a href="javascript:void(0);" onclick="itemRemove(' + length + ')" style="padding-left: 10px;" id="itemName' + length + '">移除</a>'
        $('#inputItems').append(html);
    });

    $("#selectItem").on('click', function () {
        $('#selectItem').hide();
        var html = '<div id="query_select"><select id="query_one" class="form-control" style="width: 100px;float: left;" ></select>'
            + '<select id="query_two" class="form-control" style="width: 100px;float: left;" ></select>'
            + '<select id="query_three" class="form-control" style="width: 100px;float: left;" ></select>';
        html += '<a href="javascript:void(0);" onclick="selectRemove()" style="padding-left: 10px;" id="selectName">确定</a></div>';
        $('#inputActivityRange').append(html);
        var queryOne = $("#hideQueryOne").val();
        initQueryOne("query_one", queryOne);
        $("#query_one").on("change", function () {
            $("#query_one").val($(this).val());
            initQueryTwo(this, "query_two");
        });
        $("#query_two").on("change", function () {
            $("#query_two").val($(this).val());
            initQueryThree();
        });
    });

});

function selectRemove() {
    $('#selectItem').show();
    var one = $('#query_one').val();
    var two = $('#query_two').val();
    var three = $('#query_three').val();
    if (three !== undefined && three !== null && three.trim() !== '') {
        $('#inputRanging').val(three);
    } else if (two !== undefined && two !== null && two.trim() !== '') {
        $('#inputRanging').val(two);
    } else if (one !== undefined && one !== null && one.trim() !== '') {
        $('#inputRanging').val(one);
    }
    $('#query_select').remove();
}

function itemRemove(index) {
    $('#itemName' + index).remove();
    $('#inputItem' + index).remove();
}

function initQueryOne(itemId, applyId) {
    var param = {};
    if ((applyId !== undefined) && (applyId != null)) {
        param['department'] = applyId;
    }
    $("#" + itemId).empty();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/manager/department",
        data: param,
        success: function (res) {
            if (res.code === 1) {
                var data = res.data;
                $("#" + itemId).append("<option value=''>请选择</option>" +
                    "<option value='全体学生'>全体学生</option>");
                if (data) {
                    for (var i = 0; i < data.length; i++) {
                        var key = data[i];
                        $("#" + itemId).append("<option value='" + key + "'>" + key + "</option>");
                    }

                }
            } else {
                bootbox.dialog({
                    message: res.message,
                    title: "获取学院类型异常",
                    buttons: {
                        main: {
                            label: "确定",
                            className: "btn-primary",
                            callback: function () {

                            }
                        }
                    }
                });
            }
        }
    });
}

function initQueryTwo(obj, itemId) {
    $("#" + itemId).empty();
    var department = $(obj).val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/manager/professional",
        data: 'department=' + department,
        success: function (res) {
            if (res.code === 1) {
                var data = res.data;
                $('#' + itemId).append("<option value=''>请选择</option>");
                if (data) {
                    for (var i = 0; i < data.length; i++) {
                        var val = data[i];
                        $("#" + itemId).append("<option value='" + val + "'>" + data[i] + "</option>");
                    }
                }
            } else {
                bootbox.dialog({
                    message: res.message,
                    title: "获取专业异常",
                    buttons: {
                        main: {
                            label: "确定",
                            className: "btn-primary",
                            callback: function () {

                            }
                        }
                    }
                });
            }
        }
    });
}

function initQueryThree() {
    $("#query_three").empty();
    var department = $("#query_one").val();
    var professional = $("#query_two").val();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/manager/classRoom",
        data: 'department=' + department + '&professional=' + professional,
        success: function (res) {
            if (res.code === 1) {
                var data = res.data;
                $('#query_three').append("<option value=''>请选择</option>");
                if (data) {
                    for (var i = 0; i < data.length; i++) {
                        var val = data[i];
                        $("#query_three").append("<option value='" + val + "'>" + data[i] + "</option>");
                    }
                }
            } else {
                bootbox.dialog({
                    message: res.message,
                    title: "获取班级异常",
                    buttons: {
                        main: {
                            label: "确定",
                            className: "btn-primary",
                            callback: function () {

                            }
                        }
                    }
                });
            }
        }
    });
}
